package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-01
 * Time: 23:59
 */
public class BorrowedOperation implements Ioperation {
    /*@Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        Scanner scanner = new Scanner(System.in);
        //error：System.out.println("请输入您要借阅的书名：");
        System.out.print("请输入您要借阅的书名：");
        String name = scanner.nextLine();

        int currentSize  = bookList.getUsedSize();

        //遍历书架，看书本是否已借出：
        for (int i = 0; i < currentSize; i++) {
            //用下标来访问接口数组
            Book book = bookList.getBooks(i);

            if(book.getName().equals(name)) {
                if(book.isBorrowed()) {//book.isBorrowed() 初始值是false
                    System.out.println("这本书已经被借出去了!");
                    return;
                }
            }
            //把isBorrowed 的状态写成已经借出：
            book.setBorrowed(true);
            System.out.println("借阅成功！");
            return;
        }
        //走到这里 证明书架上没有这本书
        System.out.println("没有您要借阅的这本书!");
    }*/
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");

        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入您借阅的书名：");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);

            if(book.getName().equals(name)) {
                if(book.isBorrowed()) {
                    System.out.println("这本书已经被借出了！");
                    return;
                }
                book.setBorrowed(true);
                System.out.println("借阅成功！");
                return;
            }
        }
        System.out.println("没有你要借阅的这本书....");
    }
}
